'use strict'

const Service = require('egg').Service;

class HomeService extends Service {
    async getTopBar() {
        const { app } = this;
        return await app.mysql.select('top_bar');
    }
    async getHomeData(top_id, page_num) {
        const { ctx, app } = this;
        page_num = page_num || 1;
        
        if(top_id === '1') {
            const commoDityData= await app.mysql.select('goods',{
                limit: 10,
                offset: (page_num - 1) * 10,
            });
            if(page_num !== '1'){
                return commoDityData;
            }
            const data= (await ctx.curl("http://localhost:7001/public/json/home_1.json", { dataType: 'json' })).data;
            data.map(item => {
                if(item.type==="commoDityData"){
                    item.data=commoDityData;
                }
            })
            return data;
        }
        if(top_id!=='1'){
            const commoDityData= await app.mysql.select('goods',{
                limit: 10,
                offset: (page_num - 1) * 10,
            });
            if(page_num !== '1'){
                return commoDityData;
            }
            const data= (await ctx.curl("http://localhost:7001/public/json/home_2.json", { dataType: 'json' })).data;
            data.map(item => {
                if(item.type==="commoDityData"){
                    item.data=commoDityData;
                }
            })
            return data;
        }
        
        // if (top_id === '1') {
        //     if (page_num !== '1') {
        //         return (await ctx.curl("http://localhost:7001/public/json/home_1_add.json", { dataType: 'json' })).data;
        //     }

        //     return (await ctx.curl("http://localhost:7001/public/json/home_1.json", { dataType: 'json' })).data;
        // }
        // if (top_id !== '1') {
        //     if (page_num !== '1') {
        //         return (await ctx.curl("http://localhost:7001/public/json/home_1_add.json", { dataType: 'json' })).data;
        //     }
        //     return (await ctx.curl("http://localhost:7001/public/json/home_2.json", { dataType: 'json' })).data;
        // }
    }
}

module.exports = HomeService;
